﻿using System;
using log4net;
using log4net.Appender;
using log4net.Layout;

namespace Objectware.Silverlight.Testing.Runner
{
	public static class Logging
	{
		private static readonly ILog logger = LogManager.GetLogger(typeof(Logging));

		public static string BasePath = "c:\\logs";


		public static void Info(this ILog log, string format, params string[] args)
		{
			log.Info(string.Format(format, args));
		}
		public static void Warn(this ILog log, string format, params string[] args)
		{
			log.Warn(string.Format(format, args));
		}
		public static void Fatal(this ILog log, string format, params string[] args)
		{
			log.Fatal(string.Format(format, args));
		}


		public static ILog GetLogger<T>()
		{
			var logger = LogManager.GetLogger(typeof (T));
			Configure<T>();
			return logger;
		}


		public static void Configure<T>()
		{
			var type = typeof (T);
			var name = type.FullName;
			object logging = AppDomain.CurrentDomain.GetData(name);
			if( null == logging )
			{
				//log4net.Config.XmlConfigurator.Configure(new FileInfo("c:\\log4net.config"));

				var appender = new RollingFileAppender();
				appender.File = string.Format("{0}\\{1}.txt", BasePath, type.Name);
				appender.AppendToFile = true;
				appender.MaximumFileSize = "1000000";
				appender.RollingStyle = RollingFileAppender.RollingMode.Size;
				appender.MaxSizeRollBackups = 10;
				appender.StaticLogFileName = true;
				var layout = new PatternLayout();
				layout.Header = "[Header]\n";
				layout.Footer = "[Footer]\n";
				layout.ConversionPattern = "%date [%thread] %-5level %logger [%ndc] - %message%newline";
				appender.Layout = layout;
				log4net.Config.BasicConfigurator.Configure(appender);
				logger.Info("Hello world");

				AppDomain.CurrentDomain.SetData(name,true);
			}
		}
	}
}